<?php

function exportcsv()
{

	header('Content-type: text/csv');
	header('Content-Disposition: attachment; filename="monitorIDs.csv');
	$fname = 'php://output';
	$item = $_POST['download'];
	$fp = fopen($fname,'w');
	$db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
	mysql_select_db("JRtu434", $db);
	$id = mysql_query("Select rid from projects where name=\"".$_POST["download"]."\"");
	$row = mysql_fetch_array($id);
	$sql_str = "";
	if(mysql_num_rows($id)>1)
	{
		$sql_str = " in (\"".$row[0]."\",";
		$i = 1;
		while($i < mysql_num_rows($id)-1)
		{
			$row = mysql_fetch_array($id);
			$sql_str .= "\"".$row[0]."\",";
			$i++;
		}
		$row = mysql_fetch_array($id);
		$sql_str .= "\"".$row[0]."\")";
	}
	else
	 $sql_str = "=\"".$row[0]."\"";

	$mids = mysql_query("Select ID,monID,status from testredirect where rid".$sql_str);
	$list = array();
	fputcsv($fp, array("ID","mid","status"));
	while ($row2 = mysql_fetch_array($mids))
	{	
		fputcsv($fp, array($row2[0],$row2[1],$row2[2]));	
	}
	mysql_close();
	fclose($fp);
	exit;
}


if(isset($_POST["submit"]))
{
	// check for closing projects
	if(isset($_POST["close"]))
	{
		$box=$_POST["close"];
		for ($i=0; $i < count($box); $i++)
		{ 
		 $db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
		 mysql_select_db("JRtu434", $db);
		 mysql_query("Update projects set state=\"closed\" where name=\"".$box[$i]."\"");	
		}
	}
	if(isset ($_POST["download"]))
	{
		exportcsv();
		
	}	
}
 $db = mysql_connect("mysql513.in.shared-server.net:16033", "JRtu434", "qvoiceDB1");
 mysql_select_db("JRtu434", $db);
 $result = mysql_query(
 "Select projects.ID as ID,name as name,cpc,
  cpc*sum(if(status=\"complete\",1,0)) as total,
  sum(if(status=\"complete\",1,0)) as complete,
  sum(if(status=\"screenout\",1,0)) as screenout,
  sum(if(status=\"quotafull\",1,0)) as quotafull, 
  sum(if(status=\"short\",1,0)) as short,
  sum(if(status=\"gate\",1,0)) as gate,
  sum(if(status is null,1,0)) as empty, 
  sum(if(status=\"complete\",1,0))/(sum(if(status=\"complete\",1,0))+sum(if(status=\"screenout\",1,0)))*100 as IR
  from testredirect,projects where testredirect.rid=projects.rid and state=\"live\" group by name");

 
?><!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html lang="ja"> 
 
<!-- ==== document header ==== --> 
<head> 
	<script type="text/javascript">
		function showProjects(str)
		{
		if(document.F1.lang.checked)
		{	lang="ja";}
		else
		{	lang="en";}
		if (str=="asfd")
		  {
		  document.getElementById("change").innerHTML="";
		  return;
		  }
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
		    document.getElementById("change").innerHTML=xmlhttp.responseText;
		    }
		  }
		 
		xmlhttp.open("GET","dynamic.php?func=1&state="+str+"&lang="+lang,true);
		xmlhttp.send();
		}
	function changetable(str)
		{
			if(document.F1.lang.checked)
		{	lang="ja";}
		else
		{	lang="en";}
		if (str=="asfd")
		  {
		  document.getElementById("change").innerHTML="";
		  return;
		  }
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
		    document.getElementById("change").innerHTML=xmlhttp.responseText;
		    }
		  }
		 
		xmlhttp.open("GET","dynamic.php?func=2&id="+str+"&status="+document.F1.users.value+"&lang="+lang,true);
		xmlhttp.send();
		}
	function showids(str)
		{
		if (str=="asfd")
		  {
		  document.getElementById("change").innerHTML="";
		  return;
		  }
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
		    document.getElementById("change").innerHTML=xmlhttp.responseText;
		    }
		  }
		 
		xmlhttp.open("GET","dynamic.php?func=4&id="+str,true);
		xmlhttp.send();
		}	
	function editme(str)
		{
		if (str=="asfd")
		  {
		  document.getElementById("change").innerHTML="";
		  return;
		  }
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
		    document.getElementById("change").innerHTML=xmlhttp.responseText;
		    }
		  }
		 
		xmlhttp.open("GET","dynamic.php?func=3&id="+str,true);
		xmlhttp.send();
		}	
	
		
	</script>	
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<meta http-equiv="Content-Style-Type" content="text/css"> 
<meta http-equiv="Content-Script-Type" content="text/javascript"> 
<title>Running Projects</title>
</head>
<?php include "layout.php"; ?> 
	<body>
		<div id="content">
				<form name="F1">
						<select name="users" onchange="showProjects(this.value)">
							<option value="live">Live</option>
							<option value="all">All</option>
							<option value="closed">Closed</option>
							<option value="temp">Temp Closed</option>
							<option value="po">Waiting for PO</option>
							<option value="ids">Waiting for IDs</option>
							<option value="inv">Waiting for Invoice</option>
							<option value="pay">Waiting for payment</option>
							<option value="test">TEST</option>
							<option value="repo">Report</option>						
						</select>
						<input type="checkbox" name="lang" value="ja" onclick="showProjects(document.F1.users[document.F1.users.selectedIndex].value)">日本語</input>
					</form>
						<div id="change"><?php include "dynamic.php"; drawtable("live","en");
						?></div>
					</div>
	</body>
</html>
